package com.combetter.changxing.net;

import android.content.Context;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import okhttp3.ResponseBody;

/**
 * @describe
 */

public abstract class FileDownLoadObserver<T> {

//可以重写，具体可由子类实现

    public void onComplete() {

    }

    //下载成功的回调

    public abstract void onSuccess(T t);

    //下载失败回调

    public abstract void onFail(Throwable throwable);

    //下载进度监听

    public abstract void onProgress(int progress, long total);


    /**
     * 将文件写入本地
     *
     * @param responseBody 请求结果全体
     * @param destFileName 目标文件名
     * @return 写入完成的文件
     * @throws IOException IO异常
     */

    public File saveFile(Context context, ResponseBody responseBody, String destFileName) throws IOException {
        InputStream is = null;
        byte[] buf = new byte[1024];
        int len = 0;
        FileOutputStream fos = null;
        try {
            is = responseBody.byteStream();
            final long total = responseBody.contentLength();
            long sum = 0;
            File dir = new File(context.getExternalFilesDir(null) + File.separator);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File filelist[] = dir.listFiles();
            for (File f : filelist) {
                f.delete();
            }
            File file = new File(dir, destFileName);
            fos = new FileOutputStream(file);
            while ((len = is.read(buf)) != -1) {
                sum += len;
                fos.write(buf, 0, len);
                final long finalSum = sum;
                onProgress((int) (finalSum * 100 / total), total);
            }
            fos.flush();
            return file;
        } finally {
            try {
                if (is != null) is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null) fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
